<!--新增父项目-->
<template>
  <div class="app-container-add">
    <el-row :gutter="10" class="button-wrap">
      <el-col :span="1.5">
        <el-button icon="el-icon-arrow-left" size="middle" @click="handleReturn()">返回</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-clickForbidden icon="el-icon-document-checked" size="middle" @click="handleSave">保存</el-button>
      </el-col>
    </el-row>
    <div ref="mainCont" class="main-content">
      <el-card class="box-card" shadow="never">
        <div slot="header" class="clearfix">
          <span>{{ title }}</span>
        </div>
        <div class="main-content-body">
          <el-form
            ref="formData"
            class="data-form"
            :model="formData"
            :rules="rules"
            label-width="80px"
          >
            <el-form-item label="静探报告" prop="appFiles">
              <FileUplodeOne
                ref="fileUplode"
                :read-only="false"
                :edit-file-list="FileList"
                @getFileIdsList="getFileList"
              />
            </el-form-item>
          </el-form>
          <StaticExploration
            ref="StaticExploration"
            :table-data="formData.staticTests"
          />
        </div>
      </el-card>
    </div>

  </div>
</template>

<script>

import { getFileDataList, addStaticProbe, detailStaticProbe } from '@/api/projectManage/project/exploration'
// 静探列表
import StaticExploration from './StaticExploration'
// 上传文件
import FileUplodeOne from '@/components/Upload/FileUplodeOne'
export default {
  name: 'Add',
  components: {
    FileUplodeOne,
    StaticExploration
  },
  data() {
    return {
      title: '静探报告',
      /* 新增 start*/
      formData: { // 新增数据
        explorationId: '',
        proId: '',
        appFiles: '',
        staticTests: []
      },
      rules: { // 表单校验
        appFiles: [{ required: true, message: '静探报告不能为空', trigger: 'blur' }]
      },
      /* 新增 end*/
      FileList: []// 附件
    }
  },
  mounted() {
    // 新增/编辑
    this.handleAdd()
  },
  methods: {
    // 新增/编辑
    handleAdd() {
      const { explorationId, proId, staticTestFile } = this.$route.query
      this.formData.explorationId = explorationId
      this.formData.proId = proId
      if (staticTestFile) this.handleUpdate(staticTestFile)
    },
    async handleUpdate(id) {
      const { data } = await detailStaticProbe(id)
      if (data) {
        this.FileList = [data]
      }
    },
    // 保存
    handleSave() {
      this.$refs.formData.validate(valid => {
        if (valid) {
          addStaticProbe(this.formData).then(res => {
            if (res) {
              this.msgSuccess('保存成功')
              this.handleReturn()
            }
          })
        }
      })
    },
    // 返回
    handleReturn() {
      this.$router.go(-1)
    },
    /* 新增/编辑弹层 end*/
    // 接受文件上传 组件 回传过来的文件id
    getFileList(fileIdsList) {
      this.formData.appFiles = fileIdsList
      if (fileIdsList.length) {
        getFileDataList(fileIdsList[0].fileId).then(res => {
          if (res.data) {
            this.formData.staticTests = res.data
          }
        })
      } else {
        this.formData.staticTests = []
      }
    }
    /** ************ 文件上传end *******************/
  }
}
</script>

<style lang="scss">

.area-wrap {
  margin-bottom: 30px;
  .el-form-item  > .el-form-item__content {
    margin-left: 10px !important;
  }
  .agreement {
  color: #1890ff;
  }
}
</style>
